package com.gelsanalyzer.mvc.model;

import java.beans.*;

/**
 * This class provides the base for which all of the com.gelsanalyzer.model
 * concrete classes should derive their functionality from. With this class we
 * have the ability to be observed and to notify the observers of changes to the
 * model.

 * @author Jared Olhoft
 */
public abstract class AbstractModel implements IModel {
    private PropertyChangeSupport propertyChangeSupport;

    /**
     * Default constructor. This constructor just instantiates the
     * PropertyChangeSupport field.
     */
    public AbstractModel() {
        propertyChangeSupport = new PropertyChangeSupport(this);
    }

    /**
     * This method adds a property change listener to the collection of
     * observers that have registered themselves with this class.
     * 
     * @param listener The property change listener
     */
    public void addPropertyChangeListner(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * This method removes a property change listener from the collection of
     * observers that have registered with this class.
     * 
     * @param listener The property change listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    /**
     * Fires an event to all registered listeners informing them that a property
     * in this model has changed.
     * 
     * @param propertyName The name of the property
     * @param oldValue The previous value of the property before the change
     * @param newValue The new property value after the change
     */
    protected void firePropertyChange(String propertyName,
                                      Object oldValue,
                                      Object newValue) {
        propertyChangeSupport.firePropertyChange(propertyName, oldValue,
            newValue);
    }

    public abstract void initData();

}
